home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Textfiles / Programmer's Digest 1.0.sit / The Programmers Digest™ 1.0.rsrc / TEXT_132.txt < prev    next >
Text File  |  1998-11-11  |  15KB  |  210 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11. By:Alex Sink (HardCoded‚Ć)
  12. Edited By: Cerebus and ZiffDuck
  13.  
  14.  
  15.     In this article I hope to cover the basics of the basics, ANSI C.  However, you must keep in mind that this is just an intro to C, and you may not completely understand what I say at first, but as you read on, things will make more since.
  16.  
  17.     First let's get the name straight, for you beginners.  The ANSI (Accredited Standards Committee) standard is an attempt to make C code portable (movable across many platforms, so that you only have to recompile the same code, instead of having to completely re-write the program).  C evolved from a language called B (actually BCPI-a split off of B), so it seemed natural to call it C.
  18.     Also, when you write C programs, your computer does not understand them.  You must have a compiler to translate your program into machine code.  Once it has been translated to machine code, it can be run like any other program, and understood by your computer.
  19.  
  20.     Usefulness of C: 
  21.         C is used for EVERYTHING.  C is a very powerful language that is used for things ranging from microcontroller programming to CGI programming.   Also, if you are a C++ programmer you must understand the style and ways of C; because almost all existing header files and external libraries are programmed in C.  C is very efficient, giving a ratio of 2.5 Assembly (a computer language that works directly with hardware) commands for every one C command (compared to up to 25 in some languages!).  
  22.         C programmers crave efficiency and love finding new ways to do things.  C is a useful language that all programmers must know, and almost all other languages are made so that C programmers will have an easy transition to the other language.  It is  still the basis for one of the main Computer Science classes; which will soon change over to C++,which is not too hard if you know C already.  And best of all, if you are into computer security,  almost all of your exploits and patches are written in C, so it's a good thing to understand it and be able to use it.
  23.  
  24.  
  25.                 ON TO THE MEAT:
  26.  
  27.     First, you must have an ANSI compliant C (or C++) compiler and most likely an ascii text editor.  All you have to do is plug in some C code and your compiler, when executed and pointed to your file, will turn it into machine-readable code, and your program will be made.
  28.     -make the file hello.c with the contents:
  29.  
  30. /* Yay! It's my first program! This is a comment inside your code */
  31. #include <stdio.h>
  32. main()
  33. {
  34.     printf("Hello World!\n");
  35.     return 0;
  36. }
  37.  
  38.     -put this file into your compiler and your done with your first program
  39.  
  40.     Line by line and from top to bottom (the way the code is read by your computer):
  41.  
  42.             /* Yay! It's my first program! This is a comment inside your code */
  43.     This is a comment.  It is used so you can read, in plain English -or any other language for that matter, what you tried to write the night before.  Whatever is between /* -blah- */ is ignored by compiler (computer) and is only used for making the code easy to understand.
  44.  
  45.  
  46.         #include <stdio.h>
  47.     Basically this tells your compiler that you want everything in stdio.h to be added to your file.  The '#include' statement tells your compiler to add whatever code is in <*.h> (header files containing preexisting code) or <*.c> to your file.  stdio.h is a file that contains needed information to your basic program.   
  48.  
  49.  
  50.       main()
  51.     This is the only thing that is always read in your program.  "main()" holds your whole program.  This is the heart and soul of your program, experienced programmers try to keep it short and sweet using functions.
  52.  
  53.             {
  54.     This tells your program to group everything together after it as one thing.  And because main() technically only reads one thing, this statement is always necessary.
  55.  
  56.  
  57.             printf("Hello World!\n");
  58.     This is a Function call to printf().  A function contains a group of code, so that your main() statement can be easily managed, so you can use it without worrying about how it works (term-encapsulation- when you do not have to worry about the implementation details of a function to be able to use it).  Anything inside the parentheses,"()"s, is printed directly to the screen.  SO this bit of code prints "Hello World!" because that is what is inside the parentheses.  printf("HardCoded is cool") would print -HardCoded is cool- (without the dashes of course).  The '\n' character simply makes a new line, so the output doesn't look so messy.
  59.  
  60.     The little ";" at the end tells your program that this is a single, complete statement.  If you do not put a semi-colon after a statement(any sort of command) it will cause your compiler to produce errors.  Your computer is dumb, so it would read:
  61. (an example)
  62. printf("Hello World! \n")
  63. return 0;
  64.     As ONE complete statement, even though it's not.  This is a common error, so watch out for it. It gets very easy to leave out a semi-colon and have to re-compile your whole program because of it... which becomes a pain in the rear really quick, especially since compiling large programs can take a long time.
  65.  
  66.  
  67. --->return 0;
  68.     Basically this tells your computer everything closed with no problems, if it gets this far, that is.  This tells the main() function (yes it's a function)  to represent the value 0 (essentially) which, when read by your OS, telling it that it exited normally and that everything is OK.  This statement exits the main() function, so always put it last.
  69.  
  70.  
  71. --->}
  72.     This ends the BLOCK STATEMENT that the "{" started.  Everything between the { and } operators (built-in functions) is treated as one statement by the function that contains it(in this case, the main() function.
  73.  
  74. There's your first program.
  75.     The output is very simple:"Hello World", but you are well on your way to living your whole life in a basement, where all you want to do is "add that new feature".  ;-P
  76.  
  77.     ------------------------------------------------------------
  78.  
  79.  
  80.  
  81. Alrighty then.  It's time to learn more fun stuff. Your second program will ask your users name, and then tell them to have a nice day.  Be careful, this is a little complex.  Remember that the best way to learn, is by messing with the code yourself, and soon you will understand how to use it.
  82.  
  83. ------------------starting code:
  84. /*
  85. most programmers like to say what the program is about at the top so they will know what the program does without having to look through all of the source.
  86.  
  87. Greeter program-->written by HardCoded
  88. asks for someone's name, and tells them to have a nice day.
  89. 11/3/98 last change date
  90. */
  91.  
  92. #include <stdio.h>
  93.  
  94. int main(void)
  95.     {
  96.     char tempchar;
  97.     printf("What is your name?");
  98.     scanf("%c", &tempchar);
  99.     while(tempchar != '\n')
  100.         {
  101.             printf("%c", tempchar);
  102.             tempchar = ' ';
  103.             scanf("%c", &tempchar);
  104.         }
  105.     printf(", have a nice day.");
  106.     return 0;
  107.     }    
  108. ---------------------------
  109.                                                         Output
  110. "What is your name?" (I type)->Alex
  111. (returned) "Alex, have a nice day."
  112.  
  113.  
  114.                                                     Analysis
  115.  
  116.     Now that didn't hurt, now did it?  It has some new terms(isn't pretty much everything a new term to you?), but it's cool.
  117.  
  118. Line By Line:
  119.  
  120.             /*
  121.         most programmers like to say what the program is about at the top
  122.         so they can understand what the program does without having to look at all of the code.
  123.         Greeter program-->written by HardCoded  
  124.         asks for someone's name, and tells them to have a nice day.
  125.         11/3/98 last change date
  126.             */
  127.  
  128.                     I consider this one line, because it acts as a single comment.  Everything within the /*  */ marks are ignored.   These comments are used throughout code to help clarify the code, tell you what the code assumes(in big projects), and tell you anything else (maybe a joke, if it's a really long program).
  129.  
  130.             int main(void)
  131.  
  132.                     This is declaring your main() function.  This tells your computer (compiler) to look here.  Main() is the only function that is always read.  Int is the variable returned to your OS, and void is what is going into the function, but I will cover that in more detail when I do functions.
  133.  
  134.             {
  135.  
  136.                     This tells your compiler (computer) to start the main() function and that everything inside of the brackets is a part of main(), until it hits an ending bracket.  The brackets tell your compiler to look at everything between this operator, and the closing bracket operator(that matches with this one) as one line of code.  So everything between them is treated as one line of code.  Declaring functions without using brackets is useful if you are a lazy coder, or making a really small project.  When making large projects, brackets make reading your code quicker, and easier.  "Time is money"
  137.  
  138.             char tempchar;
  139.  
  140.                     Something interesting!  This is declaring a variable!  YAY! Something that you can manipulate, and make whatever you want... well almost.  This particular type of variable is of type char.  This means that it only holds characters.  If you want to use text, you use this type of variable.  The variable name follows the type of variable so the syntax would be: 
  141.         char <name of char>; 
  142.             "tempchar" is the name of the variable.... for reasons that will become apparent. You name these guys whatever you want, but be careful not to name them a or b- stuff like that, unless you only use them for one small temporary job.  It's best to name them whatever they are holding.  Also, the name of the variable can't have spaces.
  143.  
  144.         printf("What is your name?");
  145.  
  146.             printf() once again is a basic print function printf stands for print formatted.
  147.  
  148.         scanf("%c", &tempchar);
  149.  
  150.          alright... take this one at a time:
  151.  
  152.     Scanf() - This searches the keyboard.  It takes input, basically.  What they type in gets put inside the variable that is inside the parentheses.  So it works like this.  
  153.                         scanf( &varname ); 
  154.      But the scary thing is that it doesn't actually read the characters until the user his the <enter> key, which is represented by the "\n" or newline character.  So when you hit enter, you are really sending a newline, \n, character to your computer, and telling it to read what you just wrote. In summary, if I type 5 and then enter one I am sending 5 and \n to my computer and then the variable takes on the character value of 5, not the numerical value.... but that's a different mess.
  155.     "%c", - what kind of variable are we searching for?  %c or character (numbers are originally inputted as characters too!) variable. The %c then puts the data into the first variable (since it is the first control character) after the comma, which happens to be tempchar.
  156.     "&tempchar"- alright... so what is that & thingy doing?  It's the reference operator... but for now, all you need to know is that you have to use it when using scanf(),or else your program will have some major problems, and it won't work.
  157.  
  158.  
  159. while(tempchar != '/n')
  160.  
  161.         Your program normally reads things from top to bottom, and then quits.  This kind of Control Statement is what changes that.  
  162.     while() - the statement block(what's in the {}'s)  of the while statement gets executed when what's in the parentheses is evaluated as true, or as anything but zero.  So if you put while(1), then the statement block  of the while statement will keep on executing over and over again.
  163.          (tempchar != '/n') - what the heck am I trying to say by this?  Tempchar is the value that I just scanned in using scanf().  So, if your victim typed in Suzie, my first character would be S.  The -- != '/n' -- part, translated means is not assigned to /n(<enter>).  
  164.         The " ! " means not. 
  165.         The "=" means assigned to (different than 'equals to'), but when used with another operator like this, it evaluates whether it is true or false and returns a one(true) or a zero(false).  
  166.         '/n' means, the character "/n" you use single quotes to indicate an individual character.  So, while tempchar is not enter, it will keep executing.  
  167.         In my Suzie example, it would say no, S is not a newline(/n) character, so it evaluates as true(non-zero).  The statement that the first character would make is while(1), and that executes the code inside the statement block(the stuff between the { } 's ).   Next is 'u',then 'z', then 'i', and 'e'  these would react the same way as an S, for they are not '/n' characters.  They would all run the code in the statement block (which prints the character in tempchar)  
  168.         Then... suddenly at the end of Suzie there is a /n character... so what was actually typed was Suzie/n(the /n counts as one character).  When it sees the /n character, it says,"no way!  I know you are a /n character... you are a ZERO!!! You are false!" and skips what is inside the statement block and move on to the next statement which is the printf(", have a nice day"); statement.  --What I'm trying to say in plain English- this accepts everything that you type in the keyboard, but when you hit the enter (newline) key, you make it false, and it moves on.
  169.  
  170.  
  171.         {
  172.  
  173.              This marks the beginning of the while() statement block.  Whatever is inside of it is part of the while() statement block.
  174.  
  175.  
  176.             printf("%c", tempchar);
  177.  
  178.          Something interesting about this... why isn't the tempchar inside the quotes?   Because if it was it would just print, literally tempchar.  Your computer is stupid, so it is you who has to be smart.  Taking it apart...
  179.  
  180.     printf()-you already know this... it's the same thing as before, you just put different stuff in it.
  181.  
  182.     "%c", -  Holy moley! What does that mean?  just tells your computer to look for the character variable and print it's value.  %c tells it to look for a character.  You can use %d to look for a variable type  operator... etc... I'll go over those as we come to them.  You use the comma to separate the parentheses and the variable, once again because your computer is stupid and needs a signal for where to look for the variable.
  183.  
  184.     tempchar - this variable is read from the "%c" statement.  Whatever it's value is, is printed.
  185.  
  186.  
  187.             scanf(&tempchar);
  188.  
  189.         Just using the scanf() statement to read the next character, because tempchar(because it is just a normal char and not a string)  can only hold one character.  Strings make your life easier when doing this sort of thing, but this method is more simple, and can be useful at times.  Everything has to be read in one character at a time, and then printed because of this limitation.  However, this is a good technique to learn.
  190.  
  191.  
  192.         }
  193.  
  194. ----> this ends the while() statement block.
  195.  
  196.  
  197.     printf(", have a nice day.");
  198.  
  199.         Good ole familiar printf()... so by now the name of the person running the program should have been printed by the constant scanning and printing of the while( tempchar != '\n') loop (statement)... but it will not add a space... once again your computer is dumb, but it appears to be brilliant only because of the genius of it's programmers. (ego is good ;-).  Now that the name has been scanned in letter by letter, and printed letter by letter, you now print your message: Whoever, have a nice day.  Just a little toy, try messing around with the source, and have some fun with it!
  200.  
  201.     return 0;
  202.  
  203.          If your computer gets to this statement, everything is OK!!!!! It quits successfully and everyone is happy.
  204.  
  205.     }    
  206.  
  207.         It's time to end this program and this article.
  208.  
  209.  
  210. This is just a basic intro to intro C.  Keep reading for more on C!